// (C) 2022 Intel Corporation. All rights reserved.
// Your use of Intel Corporation's design tools, logic functions and other 
// software and tools, and its AMPP partner logic functions, and any output 
// files from any of the foregoing (including device programming or simulation 
// files), and any associated documentation or information are expressly subject 
// to the terms and conditions of the Intel Program License Subscription 
// Agreement, Intel FPGA IP License Agreement, or other applicable 
// license agreement, including, without limitation, that your use is for the 
// sole purpose of programming logic devices manufactured by Intel and sold by 
// Intel or its authorized distributors.  Please refer to the applicable 
// agreement for further details.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Name:             Hot-Plug LED control
// Description:      This module is in charge of LED control (blinking patterns or solid ON/OFF state of LEDs
//                   according to table below, for Enclosure Management LED control (for Hot Plug)
//                       Status  |   Fault  |   Locate   |    Drive Carrier Amber LED State |
//                      ====================================================================
//                          OK   |     0    |      0     |      OFF                         |
//                      ====================================================================
//                        Locate |     0    |      1     |      4 Hz Blinking Pattern       |
//                      ====================================================================
//                        Fault  |     1    |      0     |      Solid ON                    |
//                      ====================================================================
//                       Rebuild |     1    |      1     |      1 Hz Blinking Pattern       |
//                      ====================================================================
//

module hp_led_ctrl 
  (
   input iClk,
   input iRst_n,
   input iEna_n,
   input iLocate,
   input iFault,
   output reg oAmberLed
   );

   //constant values defined to generate corresponding blinking patters @ 2MHz clock freq (500 nsec period)
   localparam FOUR_HERTZ = 499_999;
   localparam ONE_HERTZ = 1_999_999;


   //internal signals definitions
   integer    cnt_1Hz, cnt_4Hz;
   reg        r1Hz, r4Hz;


   //This block is used to determine LED output (Solid ON/OFF or blinking pattern at 4Hz or 1Hz) depending on inputs iLocate and iFault
   always @(posedge iClk, negedge iRst_n)
     begin
        if (~iRst_n)
          begin
             oAmberLed <= 1'b0;       //off   Status OK
          end
        else if (iClk)
          begin
             if (iEna_n == 1'b0)
               begin
                  case({iLocate, iFault})
                    default: begin
                       oAmberLed <= 1'b0;  //off  -->   2'b00 Status OK
                    end

                    2'b11: begin
                       oAmberLed <= r1Hz; //                  Status Rebuild
                    end

                    2'b10: begin
                       oAmberLed <= r4Hz; //                  Status Locate
                    end

                    2'b01: begin
                       oAmberLed <= 1'b1;  //solid ON         Status FAULT         
                    end
                  endcase // case ({iLocate, iFault})
               end // if (iEna_n == 1'b0)
             else
               begin
                  oAmberLed <= 1'b0;  //off
               end // else: !if(iEna_n == 1'b0)
          end // if (iClk)
     end // always @ (posedge iClk, negedge iRst_n)


  //This block is used to generate the blinking patterns for 4Hz and 1Hz, values based on 2MHz clock frequency (500nsec period)        
   always @(posedge iClk, negedge iRst_n)
     begin
        if (!iRst_n)
          begin
             r1Hz <= 1'b0;
             r4Hz <= 1'b0;
             cnt_1Hz <= 0;
             cnt_4Hz <= 0;
          end
        else if (iClk)
          begin
             if (cnt_4Hz == FOUR_HERTZ)     //based on 2Mhz clk
               begin 
                  cnt_4Hz <= 0;
                  r4Hz <= ~r4Hz;
               end
             else
               begin
                  cnt_4Hz <= cnt_4Hz + 1;
               end

             if (cnt_1Hz == ONE_HERTZ)     //based on 2Mhz clk
               begin 
                  cnt_1Hz <= 0;
                  r1Hz <= ~r1Hz;
               end
             else
               begin
                  cnt_1Hz <= cnt_1Hz + 1;
               end
          end // if (iClk)
     end // always @ (posedge iClk, negedge iRst_n)


endmodule // hp_led_ctrl